package com.ruoyi.web.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BdmGood;
import com.ruoyi.system.domain.dto.BdmGoodAddDto;
import com.ruoyi.system.domain.dto.BdmGoodDto;
import com.ruoyi.system.domain.dto.PointExchangeRecordDto;
import com.ruoyi.system.domain.vo.PointExchangeRecord;
import com.ruoyi.system.service.IBdmGoodService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 会员商品Controller
 *
 * @author ruoyi
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/admin/good")
@Validated
public class BdmGoodController  extends BaseController
{
    @Autowired
    private IBdmGoodService bdmGoodService;

    /**
     * 查询代理商商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BdmGood bdmGood)
    {
        startPage();
        List<BdmGood> list = bdmGoodService.selectbdmGoodList(bdmGood);
        return getDataTable(list);
    }

    /**
     * 导出代理商商品列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdmGood bdmGood)
    {
        List<BdmGood> list = bdmGoodService.selectbdmGoodList(bdmGood);
        ExcelUtil<BdmGood> util = new ExcelUtil<BdmGood>(BdmGood.class);
        util.exportExcel(response, list, "会员等级数据");
    }

    /**
     * 获取代理商商品详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bdmGoodService.selectbdmGoodById(id));
    }

    /**
     * 新增代理商商品
     */
    @PostMapping
    public AjaxResult add(@Valid @RequestBody BdmGoodAddDto bdmGoodAddDto)
    {
        BdmGood bdmGood = BeanUtil.copyProperties(bdmGoodAddDto, BdmGood.class);
        return toAjax(bdmGoodService.insertbdmGood(bdmGood));
    }

    /**
     * 修改代理商商品
     */
    @PutMapping
    public AjaxResult edit(@RequestBody BdmGood bdmGood)
    {
        return toAjax(bdmGoodService.updatebdmGood(bdmGood));
    }

    /**
     * 删除代理商商品
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bdmGoodService.deletebdmGoodByIds(ids));
    }


    /**
     * 查询代理商商品列表 根据商品id
     */
    @GetMapping("/listByIds")
    public TableDataInfo list(@RequestBody @NotEmpty(message = "商品编号至少传一个") List<Long> bdmGoodIds)
    {
        startPage();
        List<BdmGood> list = bdmGoodService.lambdaQuery().in(BdmGood::getId, bdmGoodIds).list();
        return getDataTable(list);
    }

    /**
     * 查询代理商商品列表 筛选
     */
    @GetMapping("/sizerList")
    public TableDataInfo sizerList(@RequestBody BdmGoodDto bdmGoodDto)
    {
        startPage();
        List<BdmGood> list = bdmGoodService.lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(bdmGoodDto.getClassOneId()),BdmGood::getClassOneId,bdmGoodDto.getClassOneId())//分类
                .between(ObjectUtils.isNotEmpty(bdmGoodDto.getStartTime())&&ObjectUtils.isNotEmpty(bdmGoodDto.getEndTime()),
                        BdmGood::getCreateTime, bdmGoodDto.getStartTime(), bdmGoodDto.getEndTime())//时间
                .eq(StringUtils.isNotEmpty(bdmGoodDto.getGoodName()),BdmGood::getGoodName,bdmGoodDto.getGoodName())//名称
                .eq(ObjectUtils.isNotEmpty(bdmGoodDto.getId()),BdmGood::getId,bdmGoodDto.getId())//id
                .eq(StringUtils.isNotEmpty(bdmGoodDto.getGoodType()),BdmGood::getGoodType,bdmGoodDto.getGoodType())//商品类型
                .eq(StringUtils.isNotEmpty(bdmGoodDto.getIsInitiate()),BdmGood::getIsInitiate,bdmGoodDto.getIsInitiate())//销售 下架
                .eq(ObjectUtils.isNotEmpty(bdmGoodDto.getBeAllSoldOut()),BdmGood::getGoodInventory,0)
                .apply(ObjectUtils.isNotEmpty(bdmGoodDto.getUnderstock())," CAST(ifnull(good_inventory,0) AS SIGNED)  <  CAST(ifnull(inventory_alert,0) AS SIGNED) ")
                .list();
        return getDataTable(list);
    }


    /**
     * 积分商城兑换记录
     * @return {@link PointExchangeRecord}
     */
    @GetMapping("/pointExchangeRecord")
    public TableDataInfo pointExchangeRecord(PointExchangeRecordDto pointExchangeRecordDto)
    {
        startPage();
      List<PointExchangeRecord> list=  bdmGoodService.pointExchangeRecord(pointExchangeRecordDto);
        return getDataTable(list);
    }

    /**
     * 商品导入
     * @param file
     * @return
     */
    @PostMapping("/importProduct")
    public AjaxResult importProduct(MultipartFile file) {
        bdmGoodService.importProduct(file);
        return success();
    }
}
